package org.rcy.ruicingmarket.order;

import org.rcy.framework.api.distributed.DistributedTransaction;
import org.rcy.framework.api.distributed.DistributedTransactional;
import org.rcy.framework.api.entity.PageResult;
import org.rcy.framework.transaction.distributed.DistributedTransactionManager;
import org.rcy.ruicingmarket.address.AddressService;
import org.rcy.ruicingmarket.address.AddressVo;
import org.rcy.ruicingmarket.category.CategoryClient;
import org.rcy.ruicingmarket.category.CategoryVo;
import org.rcy.ruicingmarket.common.utils.CollectionBeanUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;


/**
*
* @author RuicyWu
*@Date 2021-12-22 14:52:50
*/
@Service
public class OrderService{

	@Autowired
	private OrderDao orderDao;
    @Autowired
    private CategoryClient categoryClient;
    @Autowired
    private AddressService addressService;
    @Autowired
    private DistributedTransactionManager distributedTransactionManager;


/**
 * 根据id查询Order
 * @param id
 * @return Order
 */
   public OrderShowResponse findById(Long id){
       Order order = orderDao.get(id);
       OrderShowResponse orderShowResponse = new OrderShowResponse();
       BeanUtils.copyProperties(order,orderShowResponse);
       orderShowResponse.setStatus(order.getStatus());
       return orderShowResponse;
   }

/**
 * 根据id删除Order
 * @param id
 */
   @Transactional(rollbackFor = Exception.class)
   public void remove(Long id){}

/**
 * 新增Order
 * @param vo
 */
   public String add(OrderAddVo vo){
       //todo 扣减库存，先到redis中扣减，加锁，扣减成功后通过mq异步同步库存量
       return null;
   }

/**
 * 修改Order
 * @param vo
 */
   @Transactional(rollbackFor = Exception.class)
   public void edit(OrderEditVo vo)
   {
       System.out.println(vo.getStatus());
       Order order = new Order();
       BeanUtils.copyProperties(vo,order);
       order.setStatus(OrderStatus.valueOf(vo.getStatus()));

   }

/**
 * 查询所有Order
 * @return List<Order>
 */
   public List<OrderShowResponse> findAll(){
       return null;
   }

   public PageResult<OrderShowResponse> queryPage(OrderQueryRequest request){
       int count = orderDao.queryPageCount(request);
       List<Order> orders = orderDao.queryPage(request);
       List<OrderShowResponse> orderShowResponses = CollectionBeanUtils.copyListProperties(
               orders, OrderShowResponse::new,
               (order, response) -> {
                   response.setStatus(order.getStatus());
               }
       );
       return new PageResult<OrderShowResponse>
               (request.getPageNo(),
                       request.getPageSize(),
                       count,orderShowResponses);
   }

   public List<OrderShowResponse> getOrdersByUid(Long uid){
       List<Order> ordersByUid = orderDao.getOrdersByUid(uid);
       return null;
   }


   public DistributedTransaction testDisT(OrderAddVo vo,DistributedTransaction transaction){
        try {
            Order order = new Order();
            order.setUid(54564564654L);
            CategoryVo categoryVo = new CategoryVo();
            AddressVo addressVo = new AddressVo();
            categoryVo.setGtxId(transaction.getGtxId());
            addressVo.setGtxId(transaction.getGtxId());
            DistributedTransaction transaction1 = new DistributedTransaction();
            transaction1.setGtxId(transaction1.getGtxId());
            categoryClient.testDisT(categoryVo);
            addressService.testDisT(addressVo,transaction1);
            orderDao.insertSelective(order);
            distributedTransactionManager.finshTransaction(transaction.getGtxId());
        }catch (Exception e){
            transaction.setRollback(true);
            transaction.setException(e);
            System.out.println(e.getMessage());
        }
        return transaction;
    }
}
